
package data_bases.beans;

import Entity.Instance;
import data_bases.business.DataInstanceBusiness;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import login.beans.ConectionBean;

/**
 *
 * @author JuanFelipe
 */
@ManagedBean(name = "instanceBean")
@SessionScoped
public class InstanceBean implements Serializable{
    private static final long serialVersionUID = 48L;
    private Instance instance;

    public InstanceBean(  ) {
        FacesContext context = javax.faces.context.FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        ConectionBean conectionBean =(ConectionBean) session.getAttribute("conectionBean");

        DataInstanceBusiness dib = new DataInstanceBusiness();
        try {
            instance = dib.getInstance( conectionBean.currentConnection );
            
            if( instance==null || instance.getVersion().equals("No Data") ){
                FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Intance Error!",
                    "Problema con encontrar datos de la instancia"));
            }
            
        } catch (SQLException ex) {
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "SQL Error!",
                    ex.getMessage()));
        }
    }
    
    
    public Instance getInstance() {
        return instance;
    }

    public void setInstance(Instance instance) {
        this.instance = instance;
    }
    
}   
